import axios from "axios";
import store from "@/store";

const request = axios.create({
  baseURL: process.env.VUE_APP_BASE_API,
  timeout: 5000,
});

request.interceptors.request.use(
  (config) => {
    const token = store.state.login.token;
    config.headers.token = token;
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

request.interceptors.response.use(
  (response) => {
    if (response.data.code === 20000 || response.data.code === 200) {
      return response.data.data;
    } else {
      return Promise.reject(response.data);
    }
  },
  (error) => {
    return Promise.reject(error);
  }
);

export default request;
